<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title><!--{if $customerDeliverInfo}-->会员号:<!--{$pcustID}--> - 修改地址<!--{else}-->会员号:<!--{$pcustID}--> - 添加地址<!--{/if}--></title>
</head>
<body>
<div style="width:100%">

    <div class="items clearfix">
        <h2 style="" class="tal">正在操作：<!--{if $customerDeliverInfo}-->会员号:<!--{$pcustID}--> - 修改地址<!--{else}-->会员号:<!--{$pcustID}--> - 添加地址<!--{/if}--></h2>
        <div class="items2">

            <form id="address_form" onsubmit="return false;">
                <table>
                    <tbody>
                    <tr>
                        <td class="tar">收货人：</td>
                        <td>
                            <input type="text" value="<!--{$customerDeliverInfo.custName}-->" name="custName" id="custName" maxlength="60" style="width:180px;height:23px;line-height:23px;">
                            <input type="hidden" name="pcustID" id="pcustID" value="<!--{$pcustID}-->">
                            <input type="hidden" name="deliverID" id="deliverID" value="<!--{$customerDeliverInfo.deliverID}-->">
                            <font color="red">*</font>
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">收货地址：</td>
                        <td>
                            <select id="provinceid" name="provinceid" style="width:130px;padding: 0px;">
                                <option value="">请选择</option>
                                <!--{foreach from=$province item=line}-->
                                <option value="<!--{$line->id}-->" <!--{if $line->id == $customerDeliverInfo.provinceid}-->selected<!--{/if}-->><!--{$line->name}--></option>
                                <!--{/foreach}-->
                            </select>

                            <select id="cityid" name="cityid" style="width:130px;padding: 0px;">
                                <!--{foreach from=$cityArr item=city}-->
                                    <option value="<!--{$city->id}-->" <!--{if $city->id == $customerDeliverInfo.cityid}-->selected<!--{/if}-->><!--{$city->name}--></option>
                                <!--{foreachelse}-->
                                    <option value="">请选择</option>
                                <!--{/foreach}-->
                            </select>

                            <select id="countyid" name="countyid" style="width:130px;padding: 0px;">
                                <!--{foreach from=$countyArr item=county}-->
                                    <option value="<!--{$county->id}-->" <!--{if $county->id == $customerDeliverInfo.countyid}-->selected<!--{/if}-->><!--{$county->name}--></option>
                                <!--{foreachelse}-->
                                    <option value="">请选择</option>
                                <!--{/foreach}-->
                            </select>

                            <input type="text" id="address" name='address' value="<!--{$customerDeliverInfo.address}-->"  maxlength="100" style="height:23px;line-height:23px;width:250px" />
                            <font color="red">*</font>
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">邮政编码：</td>
                        <td>
                            <input type="text" maxlength="6" value="<!--{$customerDeliverInfo.postalCode}-->" name="postalCode" id="postalCode" style="width:180px;height:23px;line-height:23px;">
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">手机：</td>
                        <td>
                            <input type="text" maxlength="11" value="<!--{$customerDeliverInfo.mobile}-->" name="mobile" id="mobile" style="width:180px;height:23px;line-height:23px;">
                        </td>
                    </tr>
                    <tr>
                        <td class="tar">电话：</td>
                        <td>
                            <input type="text" value="<!--{$customerDeliverInfo.phone}-->" name="phone" id="phone" style="width:180px;height:23px;line-height:23px;">
                            <font color="red">手机和电话至少填一个</font>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </form>

        </div>
    </div>

    <div class="bottom_fixbox">
        <div class="rectbox tac">
            <input type="button" class="btn" onclick="custAddress.check();" value="<!--{if $customerDeliverInfo}-->修改<!--{else}-->添加<!--{/if}-->" />&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn" onclick="E.closeWindows();" value="关闭" />
        </div>
    </div>



</div>

<script type="text/javascript">

    var deliverID = '<!--{$deliverID}-->';
    var pcustID   = '<!--{$pcustID}-->';

    //变更省份，查询出相应的城市
    $("#provinceid").change(function(){

        //获取当前选中的省份ID
        var provinceID = $(this).val();

        Flg = 1;

        //将区置空
        $("#countyid").html('<option value="">请选择</option>');

        jquery_ajax(17,{id:provinceID,flg:1},custAddress.addList);
    })

    //变更城市，查询出相应的区
    $("#cityid").change(function(){

        //获取当前选中的城市ID
        var cityID = $(this).val();

        Flg = 2;

        jquery_ajax(17,{id:cityID,flg:2},custAddress.addList);
    })

    var custAddress = {

        dt: "",

        check: function() {

            this.dt = E.getFormValues('address_form');
            var custName = $.trim( $("#custName").val() );
            var provinceid = $("#provinceid").val();
            var cityid = $("#cityid").val();
            var countyid = $("#countyid").val();
            var address = $.trim( $("#address").val() );
            var phone = $.trim( $("#phone").val() );
            var mobile = $.trim( $("#mobile").val() );
            var error_msg = "";

            if ( custName == "")
                error_msg += "收货人姓名不能为空<br />";

            if ( provinceid == "")
                error_msg += "请选择省<br />";

            if ( cityid == "")
                error_msg += "请选择市<br />";

            if ( countyid == "")
                error_msg += "请选择区<br />";

            if ( address == "")
                error_msg += "请填写收货地址<br />";

            if ( mobile == "" && phone == "")
                error_msg += "手机和电话请至少填写一个<br />";

            if ( mobile != "" && !E.isMobile(mobile))
                error_msg += "手机号码格式有误<br />";

            if ( phone != "" && !E.isPhone(phone))
                error_msg += "电话号码格式有误<br />";

            if (error_msg != "")
                E.alert(error_msg);
            else {
                this.add();
            }

        },

        add: function() {
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/customer/customer.ajax?operFlg=15",
                dataType: "JSON",
                data: this.dt,
                success: function( result_obj ) {
                    if (result_obj.code == 200) {

                        E.opener("customerAddress.load");

                        if( deliverID ){
                            E.alert(result_obj.message , 2 , "E.closeWindows()");
                        }else{
                            E.alert(result_obj.message , 2 , "custAddress.clear");
                        }


                    } else {
                        E.alert(result_obj.message);
                    }
                }
            });
        },

        addList:function( obj ){

            if( obj.code == 200 ){

                var params = obj.data;

                var html = '<option value="">请选择</option>';

                for(var i=0; i<params.length; i++){
                    html += '<option value="' + params[i].id + '">' + params[i].name + '</option>';
                }

                if( Flg == 1 ){
                    $("#cityid").html(html);
                }else if ( Flg == 2 ){
                    $("#countyid").html(html);
                }

            }else{
                E.alert('系统错误，请与管理员联系');
            }

        },

        clear:function(){
            self.location= '/quanliadmin/app/customer/customer.pscript?operFlg=14&pcustID=' + pcustID;
        }

    };

    function jquery_ajax(flg,data,func){
        $.ajax({
            type: "post",
            url: "/invokingAjax-backend/customer/customer.ajax?operFlg=" + flg,
            dataType: "json",
            data: data,
            success: function( result_obj ) {
                func(result_obj);
            },
            error : function(msg){
                alert('网络不给力，请重新尝试...');
            }
        });
    }


</script>

</body>

</html>
